package com.huzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huzz.entity.SysMenu;
import com.huzz.entity.SysRole;
import com.huzz.mapper.SysMenuMapper;
import com.huzz.model.MenuModel;
import com.huzz.service.ISysMenuService;
import com.huzz.service.ISysRoleService;
import com.huzz.util.entityutil.ModelUtil;
import com.huzz.util.tree.TreeBuilder;
import com.huzz.vo.MenuVO;
import com.huzz.util.body.DataBox;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Name 名称
 * @Description 描述
 * @Author 童年的纸飞机
 * @Since 2023-04-2023/4/27 11:14
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private ISysRoleService roleService;

    @Override
    public DataBox<String> createMenu(MenuModel menuModel) {
        return DataBox.builder(menuMapper.insert(ModelUtil.transToSimilarEntity(menuModel, SysMenu.class)) > 0, "新建成功", "新建失败");

    }

    @Override
    public IPage<MenuVO> findMenuListPage(Integer pageNum, Integer pageSize, MenuModel menuModel) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(menuModel.getName())) {
            wrapper.lambda().like(SysMenu::getName, menuModel.getName());
        }


        return TreeBuilder.builderIPageTree(menuMapper, new Page<>(pageNum, pageSize), wrapper, MenuVO.class);
    }

    @Override
    public DataBox<String> updateMenuById(SysMenu menu) {
        if (menu.getId() == null) {
            return DataBox.failedBuilder("权限id不能为空");
        }
        return DataBox.builder(menuMapper.updateById(menu) > 0, "更新成功", "更新失败");
    }

    @Override
    public DataBox<String> removeMenuById(Integer id) {
        if (id == null) {
            return DataBox.failedBuilder("权限id不能为空");
        }
        return DataBox.builder(menuMapper.deleteById(id) >0, "删除成功", "删除失败");
    }

    @NotNull
    private static List<MenuVO> getMenuVOTreList(List<MenuVO> menuVOList) {
        List<MenuVO> rootList = menuVOList.stream().filter(roleVO -> roleVO.getParentId() == 0).collect(Collectors.toList());
        List<MenuVO> allChildrenList = menuVOList.stream().filter(roleVO -> roleVO.getParentId() != 0).collect(Collectors.toList());
        rootList.forEach(item -> {
            try {
                List<MenuVO> children = TreeBuilder.builder(String.valueOf(item.getId()), allChildrenList);
                item.setChildren(children);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        return rootList;
    }

    @Override
    public List<MenuVO> findMenuVOTreeListByUserId(Integer userId) {
        List<SysRole> roleList = roleService.findRoleListByUserId(userId);
        List<Integer> roleIdList = roleList.stream().map(m -> m.getId()).collect(Collectors.toList());
        if (roleIdList.isEmpty()) {
            return new ArrayList<>();
        }
        List<MenuVO> menuVOList = menuMapper.findMenuListByRoleIds(roleIdList);

        return getMenuVOTreList(menuVOList);
    }
}
